package com.net.wanjian.networkhospitalmanager.activity;

import android.content.res.Configuration;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.aliyun.vodplayer.media.AliyunDataSource;
import com.aliyun.vodplayer.media.AliyunLocalSource;
import com.aliyun.vodplayer.media.AliyunPlayAuth;
import com.aliyun.vodplayer.media.IAliyunVodPlayer;
import com.aliyun.vodplayerview.utils.ScreenUtils;
import com.aliyun.vodplayerview.widget.AliyunScreenMode;
import com.aliyun.vodplayerview.widget.AliyunVodPlayerView;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;

import butterknife.BindView;


public class SkinActivity extends BaseActivity {

    @BindView(R.id.video_view)
    AliyunVodPlayerView mAliyunVodPlayerView;

    @Override
    protected void initData() {

        mAliyunVodPlayerView.setOnPreparedListener(new IAliyunVodPlayer.OnPreparedListener() {
            @Override
            public void onPrepared() {
                mAliyunVodPlayerView.start();
            }
        });

        /**
         * 重播的数据源监听。
         */
        mAliyunVodPlayerView.setReplaySourceCallback(new AliyunVodPlayerView.ReplaySourceCallback() {
            @Override
            public void setReplaySource() {
                //重新设置replay的数据源。
                //因为此处存在一个auth过期的问题，所以这么操作
                setPlaySource();
            }
        });
        mAliyunVodPlayerView.enableNativeLog();

        setPlaySource();
    }

    private void setPlaySource() {
        String type = getIntent().getStringExtra("type");
        if ("datasource".equals(type)) {
//        构建播放数据源
            AliyunDataSource.AliyunDataSourceBuilder aliyunDataSourceBuilder = new AliyunDataSource.AliyunDataSourceBuilder(this);
            String vid = getIntent().getStringExtra("vid");
            String keyId = getIntent().getStringExtra("keyid");
            String secret = getIntent().getStringExtra("secret");
            String playKey = getIntent().getStringExtra("playKey");
            aliyunDataSourceBuilder.setAccessKeySecret(secret);
            aliyunDataSourceBuilder.setVideoId(vid);
            aliyunDataSourceBuilder.setPlayKey(playKey);
            aliyunDataSourceBuilder.setAccessKeyId(keyId);
            aliyunDataSourceBuilder.setQuality(IAliyunVodPlayer.QualityValue.QUALITY_ORIGINAL);
            AliyunDataSource aliyunDataSource = aliyunDataSourceBuilder.build();
            mAliyunVodPlayerView.setDataSource(aliyunDataSource);
        } else if ("authInfo".equals(type)) {
            //auth方式
            //NOTE： 注意过期时间。特别是重播的时候，可能已经过期。所以重播的时候最好重新请求一次服务器。
            String vid = getIntent().getStringExtra("vid");
            String authInfo = getIntent().getStringExtra("authinfo");
            AliyunPlayAuth.AliyunPlayAuthBuilder aliyunPlayAuthBuilder = new AliyunPlayAuth.AliyunPlayAuthBuilder();
            aliyunPlayAuthBuilder.setVid(vid);
            aliyunPlayAuthBuilder.setPlayAuth(authInfo);
            aliyunPlayAuthBuilder.setQuality(IAliyunVodPlayer.QualityValue.QUALITY_ORIGINAL);
            mAliyunVodPlayerView.setAuthInfo(aliyunPlayAuthBuilder.build());
        } else if ("localSource".equals(type)) {
            //本地播放
            String url = getIntent().getStringExtra("url");
            AliyunLocalSource.AliyunLocalSourceBuilder asb = new AliyunLocalSource.AliyunLocalSourceBuilder();
            asb.setSource(url);
            mAliyunVodPlayerView.setLocalSource(asb.build());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_skin;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mAliyunVodPlayerView != null) {
            mAliyunVodPlayerView.onResume();
        }

    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAliyunVodPlayerView != null) {
            mAliyunVodPlayerView.onStop();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mAliyunVodPlayerView != null) {
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {                //转为竖屏了。
                //显示状态栏
                if (Build.DEVICE.equalsIgnoreCase("mx5")
                        || Build.DEVICE.equalsIgnoreCase("Redmi Note2")
                        || Build.DEVICE.equalsIgnoreCase("Z00A_1")) {
                    getSupportActionBar().show();
                }
                this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                mAliyunVodPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

                //设置view的布局，宽高之类
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = mAliyunVodPlayerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = (int) (ScreenUtils.getWight(this) * 9.0f / 16);
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

                //设置为小屏状态
                mAliyunVodPlayerView.changeScreenMode(AliyunScreenMode.Small);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {                //转到横屏了。
                //隐藏状态栏
                if (Build.DEVICE.equalsIgnoreCase("mx5")
                        || Build.DEVICE.equalsIgnoreCase("Redmi Note2")
                        || Build.DEVICE.equalsIgnoreCase("Z00A_1")) {
                    getSupportActionBar().hide();
                } else {
                    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    mAliyunVodPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
                }

                //设置view的布局，宽高
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = mAliyunVodPlayerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

                //设置为全屏状态
                mAliyunVodPlayerView.changeScreenMode(AliyunScreenMode.Full);
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAliyunVodPlayerView != null) {
            mAliyunVodPlayerView.onDestroy();
        }
    }
}
